﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint.ApplicationPages;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.Utilities;

namespace Rtl.SharePoint.Administration
{
    public class VEConfigPage : OperationsPage
    {
        protected InputFormTextBox txtCommonServiceEndpoint;
        protected InputFormTextBox txtAccountID;
        protected InputFormTextBox txtPassword;

        private VEConnectionInfo _settings = null;

        protected override void OnLoad(EventArgs e)
        {
            _settings = LoadSettingsInfo();

            if ((!this.IsPostBack) && (_settings != null))
            {
                txtCommonServiceEndpoint.Text = _settings.ServiceUrl;
                txtAccountID.Text = _settings.AccountID;
            }
            
            base.OnLoad(e);
        }

        private VEConnectionInfo LoadSettingsInfo()
        {
            VEConnectionInfoStorage storageHelper = new VEConnectionInfoStorage();
            return storageHelper.Settings;
        }

        protected void BtnSubmit_Click(object sender, EventArgs e)
        {
            if (_settings != null)
            {
                _settings.ServiceUrl = this.txtCommonServiceEndpoint.Text;
                _settings.AccountID = this.txtAccountID.Text;
                _settings.Password = this.txtPassword.Text;
                _settings.Update();
            }

            SPUtility.Redirect("/_admin/applications.aspx", SPRedirectFlags.Default, this.Context);
        }
    }
}
